//
//  ZYComposeToolbar.swift
//  CZ29WeiboZYH
//
//  Created by 沐颜澈 on 16/7/3.
//  Copyright © 2016年 muyanche. All rights reserved.
//

import UIKit
// toolbar 按钮枚举
enum CZComposeToolbarType: Int{
    // 图片
    case Picture = 10
    // @
    case Mention = 20
    // #
    case Trend = 30
    // 表情
    case Emoticon = 40
    // +
    case Add = 50
}



//7.4=============
class ZYComposeToolbar: UIView {
    
    //切换键盘的按钮
    var emoticonButton: UIButton?
    
    
    //在composeToolbar里面提供一个属性
    //告知外界键盘的类型
    //如果isEmotion是true显示的是表情键盘,button显示image显示键盘
    //如果isEmotion是false,代表显示的是系统的键盘,button显示image显示笑脸
    var isEmotion: Bool = false {
        didSet {
            //显示键盘
            if isEmotion {
                
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
            
            } else {
            //显示笑脸
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
            }
        
        }
        
    
    }
    
    // 告知外界按钮点击啦
    var closure:((type: CZComposeToolbarType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        // 添加控件
        let pictureButton = addChildButtons("compose_toolbar_picture",type: .Picture)
        let mentionButton = addChildButtons("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButtons("compose_trendbutton_background",type: .Trend)
        emoticonButton = addChildButtons("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = addChildButtons("compose_add_background",type: .Add)
        // 添加约束
        pictureButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
            
        }
        
    }
    // MARK: - 监听方法
    func buttonClick(button: UIButton){
        closure?(type:CZComposeToolbarType(rawValue: button.tag)!)
    }
    // 创建button按钮的公共方法
    func addChildButtons(imgName: String, type: CZComposeToolbarType) -> UIButton{
        let button = UIButton()
        
        button.tag = type.rawValue
        // 添加点击事件
        button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }
    
}
















